package com.anlogic.sdk.bootimage.internal.ui.common;

import com.anlogic.sdk.bootimage.internal.ui.composite.AbsBootgenComposite;
import com.anlogic.sdk.utils.StringUtils;
import java.util.Arrays;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

public class BootgenComboViewer<V> implements IExtBootgenField<Combo, V> {
	private ComboViewer comboViewer;

	private V[] options;

	private AbsBootgenComposite bootgenComposite;

	private IModifyListener<V> modifyListener;

	private Label label;

	public BootgenComboViewer(AbsBootgenComposite bootgenComposite) {
		this.bootgenComposite = bootgenComposite;
	}

	@SuppressWarnings("unchecked")
	public void createField(Composite container, String labelText, Object... options) {
		this.options = (V[]) options;
		if (StringUtils.isNotEmpty(labelText)) {
			this.label = new Label(container, 0);
			this.label.setText(labelText);
			GridDataFactory.swtDefaults().applyTo((Control) this.label);
		}
		this.comboViewer = new ComboViewer(container);
		this.comboViewer.setContentProvider((IContentProvider) new ArrayContentProvider());
		this.comboViewer.setLabelProvider((IBaseLabelProvider) new BootgenComboLabelProvider());
		setInput((V[]) options);
		this.comboViewer.getCombo().setData(this.label);
		this.comboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				V comboOption = BootgenComboViewer.this.getSelection();
				if (!BootgenComboViewer.this.bootgenComposite.isSkipNotifactions()
						&& BootgenComboViewer.this.modifyListener != null)
					BootgenComboViewer.this.modifyListener.valueChanged(comboOption);
			}
		});
	}

	@SuppressWarnings("unchecked")
	public void setInput(Object... options) {
		this.options = (V[]) options;
		V selection = getSelection();
		this.comboViewer.setInput(options);
		setSelection(selection);
	}

	public Label getLabel() {
		return this.label;
	}

	@SuppressWarnings("unchecked")
	public V getSelection() {
		V comboOption = (V) BootgenComboViewerUtils.getSelection(this.comboViewer, (Class<? extends Object>) this.options[0].getClass());
		return comboOption;
	}

	public void setModifyListener(IModifyListener<V> modifyListener) {
		this.modifyListener = modifyListener;
	}

	public void setSelection(V selection) {
		if (selection != null && Arrays.<V>asList(this.options).contains(selection))
			this.comboViewer.setSelection((ISelection) new StructuredSelection(selection));
		if (getSelection() == null)
			this.comboViewer.getCombo().select(0);
	}

	public Combo getControl() {
		return this.comboViewer.getCombo();
	}

	public void setDefaultGridData(int hSpan) {
		GridDataFactory.swtDefaults().span(hSpan, 1).applyTo((Control) getControl());
	}

	public void setEnabled(boolean enabled) {
		getControl().setEnabled(enabled);
		if (this.label != null)
			this.label.setEnabled(enabled);
	}
}
